/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Appointment;
import model.Campaign;
import model.Task;
import model.user.Governor;
import model.user.Staff;
import util.Utilization;
import view.user.ACIListModel;
import view.user.GovernorView;

/**
 *
 * @author CUNEYT
 */
public class GovernorController {

    private class StaffUtilizationSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getStaffUtilizationList().getSelectedValue() != null) {
                Staff s = (Staff) view.getStaffUtilizationList().getSelectedValue();
                view.getStaffTaskList().setModel(new ACIListModel(s.getTasks()));
                view.getStaffAppointmentsList().setModel(new ACIListModel(s.getAppointments()));
            } else {
                view.getStaffTaskList().setModel(new DefaultListModel());
                view.getStaffAppointmentsList().setModel(new DefaultListModel());
            }
        }
    }

    private class StaffSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getStaffList().getSelectedValue() != null) {
                Staff s = (Staff) view.getStaffList().getSelectedValue();
                view.getStaffTaskList().setModel(new ACIListModel(s.getTasks()));
                view.getStaffAppointmentsList().setModel(new ACIListModel(s.getAppointments()));
            } else {
                view.getStaffTaskList().setModel(new DefaultListModel());
                view.getStaffAppointmentsList().setModel(new DefaultListModel());
            }
        }
    }

    private class AppointmentSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getStaffAppointmentsList().getSelectedValue() != null) {
                Appointment app = (Appointment) view.getStaffAppointmentsList().getSelectedValue();
                view.getAppDateTextField().setText(Utilization.formatDate(app.getDate()));
                view.getAppStartTimeTextField().setText(app.getStartTime());
                view.getAppEndTimeTextField().setText(app.getEndTime());
                view.getAppExplanationTextArea().setText(app.getExplanataion());
            } else {
                view.getAppDateTextField().setText("");
                view.getAppStartTimeTextField().setText("");
                view.getAppEndTimeTextField().setText("");
                view.getAppExplanationTextArea().setText("");
            }
        }
    }

    private class TaskSelectedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getStaffTaskList().getSelectedValue() != null) {
                Task task = (Task) view.getStaffTaskList().getSelectedValue();
                view.getTaskStartDateTextField().setText(Utilization.formatDate(task.getDate()));
                view.getTaskEndDateTextField().setText(Utilization.formatDate(task.getEndDate()));
                view.getTaskStartTimeTextField().setText(task.getStartTime());
                view.getTaskEndTimeTextField().setText(task.getEndTime());
                view.getTaskExplanationTextField().setText(task.getExplanataion());
                if (task.getRelatedTo() != null) {
                    view.getRelatedCampaignTextField().setText(task.getRelatedTo().toString());
                }
                if (task.getSkill() != null) {
                    view.getSkillsRequiredTextField().setText(task.getSkill().toString());
                }
            } else {
                view.getTaskStartDateTextField().setText("");
                view.getTaskEndDateTextField().setText("");
                view.getTaskStartTimeTextField().setText("");
                view.getTaskEndTimeTextField().setText("");
                view.getRelatedCampaignTextField().setText("");
                view.getSkillsRequiredTextField().setText("");
                view.getTaskExplanationTextField().setText("");
            }
        }
    }

    private class NegotiationSelectionChangedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getCurrentNegotiationsList().getSelectedValue() != null) {
                Campaign c = (Campaign) view.getCurrentNegotiationsList().getSelectedValue();
                view.getNegotiationTypeTextField().setText(c.getType());
                view.getNegotiationStartDateTextField().setText(Utilization.formatDate(c.getStartDate()));
                view.getNegotiationEndDateTextField().setText(Utilization.formatDate(c.getEndDate()));
                view.getNegotiationClientTextField().setText(c.getClient().toString());
                view.getNegotiationDirectorTextField().setText(c.getDirector().toString());

            } else {
                view.getNegotiationTypeTextField().setText("");
                view.getNegotiationStartDateTextField().setText("");
                view.getNegotiationEndDateTextField().setText("");
                view.getNegotiationClientTextField().setText("");
                view.getNegotiationDirectorTextField().setText("");
            }
        }
    }

    private class CampaignSelectionChangedListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (view.getActiveCampaignsList().getSelectedValue() != null) {
                Campaign c = (Campaign) view.getActiveCampaignsList().getSelectedValue();
                view.getCampaignTypeTextField().setText(c.getType());
                view.getCampaignStartDateTextField().setText(Utilization.formatDate(c.getStartDate()));
                view.getCampaignEndDateTextField().setText(Utilization.formatDate(c.getEndDate()));
                view.getCampaignClientTextField().setText(c.getClient().toString());
                view.getCampaignClientContactTextField().setText(c.getClient().getContact_person());
                view.getCampaignDirectorTextField().setText(c.getDirector().toString());
                view.getCampaignResponsibleTextField().setText(c.getResponsible().toString());
                view.getStaffList().setModel(new ACIListModel(c.getStaff()));

            } else {
                view.getCampaignTypeTextField().setText("");
                view.getCampaignStartDateTextField().setText("");
                view.getCampaignEndDateTextField().setText("");
                view.getCampaignClientTextField().setText("");
                view.getCampaignClientContactTextField().setText("");
                view.getCampaignDirectorTextField().setText("");
                view.getCampaignResponsibleTextField().setText("");
                view.getStaffList().setModel(new DefaultListModel());
            }
        }
    }

    private class SeeStaffUtilizationActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getUtilizationPanel());
            clearListSelections();
        }
    }

    private class SeeCurrentNegotiationsActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getCurrentNegotiationsPanel());
            clearListSelections();
        }
    }

    private class SeeActiveCampaignsActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            changeForePanel(view.getActiveCampaignsPanel());
            clearListSelections();
        }
    }
    private GovernorView view = null;
    private Governor governor = null;

    public GovernorController(Governor g, GovernorView v) {
        governor = g;
        view = v;
        governor.addObserver(view);
        view.getgSeeActiveCampaignsButton().addActionListener(new SeeActiveCampaignsActionListener());
        view.getgSeeNegotiationsButton().addActionListener(new SeeCurrentNegotiationsActionListener());
        view.getgSeeStaffUtilizationButton().addActionListener(new SeeStaffUtilizationActionListener());
        view.getActiveCampaignsList().addListSelectionListener(new CampaignSelectionChangedListener());
        view.getActiveCampaignsList().setModel(new ACIListModel(governor.getActiveCampaigns()));
        view.getCurrentNegotiationsList().addListSelectionListener(new NegotiationSelectionChangedListener());
        view.getCurrentNegotiationsList().setModel(new ACIListModel(governor.getCurrentNegotiations()));
        view.getStaffList().addListSelectionListener(new StaffSelectedListener());
        view.getStaffUtilizationList().addListSelectionListener(new StaffUtilizationSelectedListener());
        view.getStaffUtilizationList().setModel(new ACIListModel(governor.getStaff()));
        view.getStaffTaskList().addListSelectionListener(new TaskSelectedListener());
        view.getStaffAppointmentsList().addListSelectionListener(new AppointmentSelectedListener());

    }

    private void clearListSelections() {
        view.getActiveCampaignsList().clearSelection();
        view.getStaffList().clearSelection();
        view.getCurrentNegotiationsList().clearSelection();
        view.getStaffUtilizationList().clearSelection();
        view.getStaffTaskList().clearSelection();
        view.getStaffAppointmentsList().clearSelection();
    }

    public Governor getGovernor() {
        return governor;
    }

    public void setGovernor(Governor governor) {
        this.governor = governor;
    }

    public GovernorView getView() {
        return view;
    }

    public void setView(GovernorView view) {
        this.view = view;
    }

    public void changeForePanel(javax.swing.JPanel forePanel) {
        view.getGovernorLayeredPane().setLayer(view.getActiveCampaignsPanel(), 0);
        view.getActiveCampaignsPanel().setVisible(false);
        view.getGovernorLayeredPane().setLayer(view.getCurrentNegotiationsPanel(), 1);
        view.getCurrentNegotiationsPanel().setVisible(false);
        view.getGovernorLayeredPane().setLayer(view.getUtilizationPanel(), 2);
        view.getUtilizationPanel().setVisible(false);
        view.getGovernorLayeredPane().setLayer(forePanel, 3);
        forePanel.setVisible(true);
    }
}
